const puppeteer = require('puppeteer');
const fs = require("fs")
const mkdirp = require("mkdirp")
const outDir = "./dist/meichuan"

const url = "http://www.dlmckj.top/";
(async () => {
    const browser = await puppeteer.launch()
    try {
        const page = await browser.newPage()
        page.setDefaultTimeout(0)

        //处理弹窗，防止弹出导致网页加载中断
        page.on('dialog', async dialog => {
            console.log(dialog.message());
            await dialog.dismiss();
        });

        page.on('response', function (response) {
            const url = response.url();
            const status = response.status()
            //DEBUG && console.log('Get:', status, url);

            //状态码判断
            if (status !== 200) {
                console.log('Get Warning:', status, url);
                if (status >= 400) return;
            }
            if (url.split("://").length < 2) {
                console.log('Save Error:', url);
                return;
            }
            let filename = url.split("://")[1].split("?")[0];
            if(filename[filename.length-1]==='/')filename+='(index)'
            const dir = filename.substr(0, filename.lastIndexOf('/'));
            response.text().then((body) => {
                mkdirp(outDir + '/' + dir).then(res =>
                    fs.writeFile(outDir + '/' + filename, body, function (e) {
                    })
                )
            })
        });

        console.log('页面开始加载');
        await page.goto(url, { waitUntil: 'networkidle2' });

    } catch (e) {
        console.log(e)
    } finally {
        await browser.close()
    }
})();